
// bfs广度遍历邻接矩阵的图
export function graphBFS<T = number>(graph: Map<T, Set<T>>, startVet: T): T[] {
    if (!graph.size || !startVet) return []
    const visited = new Set<T>()
    const res: T[] = []
    const que = [startVet]
    visited.add(startVet)
    while (que.length) {
        const val = que.shift()
        res.push(val)
        const set = graph.get(val)
        for (const item of set) {
            if (!visited.has(item)) {
                que.push(item)
                visited.add(item)
            }
        }
    }
    return res
}
// dfs广度遍历邻接矩阵的图
export function graphDFS<T = number>(graph: Map<T, Set<T>>, startVet: T): T[] {
    if (!graph.size || !startVet) return []
    const res: T[] = []
    const visited: Set<T> = new Set()
    const dfs = (graph: Map<T, Set<T>>, startVet: T, visited: Set<T>, res: T[]) => {
        res.push(startVet)
        visited.add(startVet)
        for (const vet of graph.get(startVet)) {
            if (visited.has(vet)) continue;
            dfs(graph, vet, visited, res)
        }
    }
    dfs(graph, startVet, visited, res)
    return res
}